<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <el-form-item label="客户名称">
        <el-select
          v-model="listQuery.fsiteNo"
          clearable
          filterable
          placeholder="请选择客户名称"
        >
          <el-option
            v-for="item in fsiteNoBelongOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称">
        <el-input
          v-model.trim="listQuery.fclassstname"
          clearable
          class="filter-item"
          placeholder="请输入分类名称"
        />
      </el-form-item>
      <el-form-item label="上级分类">
        <el-select
          v-model="listQuery.fparentno"
          clearable
          filterable
          placeholder="请选择上级分类"
        >
          <el-option
            v-for="item in list"
            :key="item.fclassstno"
            :label="item.fclassstname"
            :value="item.fclassstno"
          />
        </el-select>
        
      </el-form-item>
      <el-form-item>
        <el-button
          round
          size="medium"
          class="filter-item"
          @click="getList"
          :style="{ background: buttonColor.lookup, color: buttonColor.color }"
          >查找</el-button
        >
        <el-button
          round
          size="medium"
          class="filter-item"
          @click="opencreate"
          :style="{ background: buttonColor.add, color: buttonColor.color }"
          >添加</el-button
        >
        <el-button
            :style="{ background: buttonColor.import, color: buttonColor.color }"
            round
            size="medium"
            @click="handleUpload">导入</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询结果 -->
    <el-table
      :height="buttonColor.clientHeight"
      v-loading="listLoading"
      :data="list"
      stripe
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      :header-cell-style="tableColor"
    >
      <el-table-column
        align="center"
        :width="buttonColor.operationTwo"
        label="操作"
        class-name="small-padding"
      >
        <template slot-scope="scope">
          <div class="operationButton">
            <div
              :style="{ color: buttonColor.edit, cursor: 'pointer' }"
              @click="Update(scope.row)"
            >
              <img src="../../../public/images/edit.png" />
              编辑
            </div>
            <div
              :style="{ color: buttonColor.delete, cursor: 'pointer' }"
              @click="Delete(scope.row)"
            >
              <img src="../../../public/images/del.png" />
              删除
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" width="80" label="序号" type="index" />
      <el-table-column
        align="left"
        label="分类编号"
        width="271"
        prop="fclassstno"
      />
      <el-table-column
        align="left"
        label="分类名称"
        prop="fclassstname"
      />
      <el-table-column  :formatter="formatfsupno" align="left" label="上级分类" prop="fclassstno" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <!-- 新增修改 -->
    <el-dialog
      :close-on-click-modal="false"
      width="450px"
      :before-close="handleClose"
      :visible.sync="dialogFormVisible"
    >
      <template slot="title">
        <div :style="exportColor">样品分类</div>
      </template>
      <el-form
        ref="dataForm"
        :model="dataForm"
        label-position="left"
        status-icon
        label-width="90px"
        :rules="rules"
      >
        <el-form-item label="分类编号" prop="fclassstno" class="required">
          <el-input v-model="dataForm.fclassstno" :disabled="modify == 2" />
        </el-form-item>
        <el-form-item label="分类名称" prop="fclassstname" class="required">
          <el-input v-model="dataForm.fclassstname" />
        </el-form-item>
        <el-form-item label="上级分类" prop="fparentno">
          <el-input v-model="dataForm.fparentno" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          :style="{
            background: buttonColor.cancel,
            color: buttonColor.colorone,
          }"
          round
          @click="handleClose"
          size="medium"
          >取消</el-button
        >
        <el-button
          :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          round
          @click="UpdateorCreate"
          size="medium"
          >{{ modify == 1 ? "新增" : "修改" }}</el-button
        >
      </div>
    </el-dialog>
    <!-- 导出弹窗 -->
      <el-dialog :close-on-click-modal="false" :visible.sync="uploadVisible" width="auto">
        <template slot="title">
          <div :style="exportColor">样品分类</div>
        </template>
        <div style="width:300px;height:200px;display: flex;flex-direction: column;align-items: center">
          <el-upload
            :on-success="uploadFileSuccess"
            :headers="headers"
            :action="uploadPath2"
            :data="{ 'tableName': 'Import_i_ts_good_info' }"
            :show-file-list="false"
            class="filter-item"
            style="margin-top:20px"
            accept=".xlsx">
            <el-button
              :style="{ background: buttonColor.import, color: buttonColor.color }"
              round
              size="medium"
              icon="el-icon-upload">导入</el-button>
          </el-upload>
          <div style="margin-top:30px;text-decoration:underline">
            <a :href="downUrl" style="color: blue">模板文件下载</a>
          </div>
  
        </div>
      </el-dialog>
  </div>
</template>
  
<script>
import {
  tableColor,
  titleColor,
  exportColor,
  buttonColor,
} from "@/filters/index";
import { fsiteOptionsByLevel, getListArea, uploadPath2 } from "@/api/area";
import { create, list, update, Delete } from "@/api/sample/cat";
import Pagination from "@/components/Pagination";
import { getApiUrl } from '@/utils/request'
import { getToken } from '@/utils/auth'
export default {
  name: "unit",
  components: { Pagination },
  data() {
    return {
      tableColor,
      uploadPath2,
      buttonColor,
      exportColor,
      uploadVisible:false,
      modify: 1,
      listLoading: false,
      fsiteNoBelongOptions: [],
      list: [],
      downUrl: null,
      dialogFormVisible: false,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        fsiteNo: this.$store.state.user.fsiteNo,
        fparentno: undefined,
        fclassstname: undefined,
      },
      dataForm: {
        fsiteNo: this.$store.state.user.fsiteNo,
        fclassstname: undefined,
        fclassstno: undefined,
        fclassstyle: 0,
        flevel: 0,
        feffday:0,
        foperator: undefined,
        fparentno:undefined,
        fupddate:new Date(),
        foperator:this.$store.state.user.id
      },
      rules:{
         fclassstname:[{required: true, message: '分类名称不能为空' }],
        fclassstno:[{required: true, message: '分类编号不能为空' }],
      },
    };
  },
  computed: {
      headers() {
        return {
          'Authorization': getToken()
        }
      }
  },
  created() {
    this.downUrl = getApiUrl() + 'xlsx/1-2样品分类.xlsx'
    this.getcustom();
    this.getList();
  },
  methods: {
    //列表
    getList() {
      list(this.listQuery).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    //客户列表
    getcustom() {
      fsiteOptionsByLevel(3)
        .then((response) => {
          this.fsiteNoBelongOptions = response.data.data.list;
        })
        .catch(() => {
          this.fsiteNoBelongOptions = [];
        });
    },
    //点击编辑
    Update(row) {
      this.$nextTick(() => {
        this.modify = 2;
        this.dataForm.fclassstno = row.fclassstno;
        this.dataForm.fclassstname = row.fclassstname;
        this.dataForm.fparentno = row.fparentno;
        this.dialogFormVisible = true;
      });
    },
    //关闭弹框
    handleClose() {
      this.dialogFormVisible = false;
      this.$refs["dataForm"].resetFields();
    },
    //点击新增
    opencreate() {
      this.dialogFormVisible = true;
      this.modify = 1;
    },
    //新增、修改
    UpdateorCreate() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
             var formMethod = this.modify == 1 ? create : update;
          formMethod(this.dataForm)
            .then((res) => {
              this.$notify.success({
                title: "成功",
                message: "存储资料成功",
              });
              this.dialogFormVisible = false;
              this.getList();
            })
            .catch((res) => {
              this.$notify.error({
                title: "失败",
                message: "存储资料成功",
              });
            });
          }
        })
    },
    //删除
    Delete(row) {
      Delete(row)
        .then((res) => {
          this.$notify.success({
            title: "成功",
            message: "删除资料成功",
          });
          this.getList();
        })
        .catch((res) => {
          this.$notify.error({
            title: "失败",
            message: "删除资料失败",
          });
        });
    },
    //导入
    handleUpload() {
        this.uploadVisible = true
    },
    //
    uploadFileSuccess: function(response) {
        this.uploadVisible = false
        this.$notify.success({
          title: '导入',
          dangerouslyUseHTMLString: true,
          message: response.data
        })
      },
    // 供应商
    formatfsupno: function (row, column) {
      if (this.list.length > 0) {
        for (let i = 0; i < this.list.length; i++) {
          if (row.fparentno === this.list[i].fclassstno) {
            return this.list[i].fclassstname;
          }
        }
        return "";
      } else {
        return "";
      }
    },
  },
};
</script>
  